package com.kim.rssreader.activity;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kim.rssreader.R;
import com.kim.rssreader.db.DBDAO;
import com.kim.rssreader.domain.AbstractDomain;
import com.kim.rssreader.domain.RssColumn;
import com.kim.rssreader.utils.DateUtil;

public class SubscribeColumnActivity extends BaseActivity {
	private ImageView img_back;
	private ListView lv_columnList;
	private TextView txv_categoty;

	List<AbstractDomain> columnList;
	private boolean isFrequent = false;
	private String categoryId = "c00da5c4ce4b59d34c14ab18775cbe4a5aed07fc";
	private String categoryName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initView() {
		setContentView(R.layout.subscribe_column_layout);
		img_back = (ImageView) findViewById(R.id.sub_column_iv_back);
		lv_columnList = (ListView) findViewById(R.id.sub_column_lv_column_list);
		txv_categoty = (TextView) findViewById(R.id.sub_column_txv_category);

		getDataFromIntent();
		initData();
		
		txv_categoty.setText(categoryName);

		columnAdapter adapter = new columnAdapter(this, columnList, dbDAO);
		lv_columnList.setAdapter(adapter);

		// 返回键
		img_back.setOnClickListener(new ImageView.OnClickListener() {
			@Override
			public void onClick(View v) {
				SubscribeColumnActivity.this.finish();
			}
		});

		// 列表点击
		// lv_columnList.setOnItemClickListener(new
		// ListView.OnItemClickListener() {
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// RssColumn column = (RssColumn) columnList.get(arg2);
		// final String columnId = column.rssColumnID;
		// showDialog(columnId);
		// arg1.setBackgroundResource(R.drawable.add_press);
		// }
		// });

	}

	@Override
	public void initParam() {

	}

	private void initData() {
		columnList = dbDAO.getColumnByCategory(categoryId, isFrequent);
	}

	private void getDataFromIntent() {
		Bundle bundle = getIntent().getBundleExtra("ColumnInfo");
		isFrequent = bundle.getBoolean("isFrequent");
		categoryId = bundle.getString("categoryId");
		categoryName = bundle.getString("CategoryName");
	}

	class columnAdapter extends BaseAdapter {
		private Context context;
		List<AbstractDomain> column_List;
		private LayoutInflater inflater;
		private RssColumn column;
		private DBDAO dbDAO;
		private boolean added = false;

		public columnAdapter(Context ct, List<AbstractDomain> list, DBDAO db) {
			this.context = ct;
			this.column_List = list;
			this.inflater = LayoutInflater.from(context);
			this.dbDAO = db;
		}

		@Override
		public int getCount() {
			return column_List.size();
		}

		@Override
		public Object getItem(int position) {
			return column_List.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.subscribe_column_items,
						parent, false);
				holder.cloumnName = (TextView) convertView
						.findViewById(R.id.sub_column_item_name);
				holder.sub_column_add = (ImageView) convertView
						.findViewById(R.id.sub_column_add);
				// holder.isAdd = (CheckBox)
				// convertView.findViewById(R.id.ckbox_isadd);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			column = (RssColumn) column_List.get(position);
			final String name = column.rssColumnName;
			final String columnId = column.rssColumnID;
			holder.cloumnName.setText(name);

			if (dbDAO.checkIsSubcribe(columnId)) {
				// holder.isAdd.setChecked(true);
				holder.sub_column_add
						.setBackgroundResource(R.drawable.column_add_cancel);
			} else {
				// holder.isAdd.setChecked(false);
				holder.sub_column_add
						.setBackgroundResource(R.drawable.add_press);
			}

			holder.sub_column_add
					.setOnClickListener(new ImageView.OnClickListener() {
						@Override
						public void onClick(View v) {
							if (!dbDAO.checkIsSubcribe(columnId)) {
								dbDAO.insertSubscription(columnId,
										DateUtil.getCurrenTime());
								v.setBackgroundResource(R.drawable.column_add_cancel);
								Toast.makeText(context, "成功添加" + name,
										Toast.LENGTH_SHORT).show();
							} else {
								dbDAO.delSubscription(columnId);
								v.setBackgroundResource(R.drawable.add_press);
							}
						}
					});

			// holder.isAdd.setOnCheckedChangeListener(new
			// OnCheckedChangeListener() {
			// @Override
			// public void onCheckedChanged(CompoundButton buttonView, boolean
			// isChecked) {
			// if(isChecked){
			// if(!dbDAO.checkIsSubcribe(columnId)){
			// // dbDAO.delSubscription(columnId);
			// dbDAO.insertSubscription( columnId, name,
			// DateUtil.getCurrenTime());
			// }
			// Toast.makeText(context, "成功添加"+name, Toast.LENGTH_SHORT).show();
			// }else{
			// // Toast.makeText( SubscribeColumnActivity.this, "取消订阅"+name,
			// Toast.LENGTH_SHORT).show();
			// if(dbDAO.checkIsSubcribe(columnId)){
			// dbDAO.delSubscription(columnId);
			// }
			// }
			// }
			// });
			return convertView;
		}

		private class ViewHolder {
			TextView cloumnName;
			ImageView sub_column_add;
			// CheckBox isAdd;

		}

	}

}
